package com.lvyou.intercepter;

import com.lvyou.entity.Admin;
import com.lvyou.util.UrlKit;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AdminLoginInter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean res=false;
        HttpSession session=request.getSession();
        String uri=request.getRequestURI();
        System.out.println(uri);

        Admin admin=(Admin)session.getAttribute("adminInfo");
        if(admin!=null) {
            res=true;
        }else {

            String servletPath = request.getServletPath();
            if(servletPath.toLowerCase().endsWith(".html")){
                String url= UrlKit.getURL(request);
                response.sendRedirect(url+"/mgr/login.html");
                res=false;
            }else{
                request.getRequestDispatcher("/toLogin").forward(request,response);
                res = false;
            }

        }
        return  res;
    }
}
